//
//  ProgressEnquiryViewController.m
//  homeland
//
//  Created by SunShine on 13-11-2.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ProgressEnquiryViewController.h"
#import "AdvisoryListCell.h"
#import "ResidentProgressDetailViewController.h"
#import "UserInfoService.h"

@interface ProgressEnquiryViewController ()
@property (strong, nonatomic) CustomPullToRefresh *ptr;
@property (strong, nonatomic) ProgressEnquiryService *dataService;
@end

@implementation ProgressEnquiryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"居民进度查询";
    _searchBarName.inputAccessoryView = [self getInputAccessoryView];
    _searchBarNo.inputAccessoryView = [self getInputAccessoryView];
    self.dataService = [ProgressEnquiryService new];
    _dataService.delegate = self;
    //开启下拉刷新
    self.ptr = [[CustomPullToRefresh alloc]initWithScrollView:self.tableView delegate:self];
    //底部提示
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        _tipsView.text = @"如果您在个人电脑上提交的记录没有在此显示，请输入姓名+申办流水号查询";
    } else {
        _tipsView.text = @"您未登陆，请输入姓名+申办流水号查询";
    }
    //默认不显示状态条
    self.statusView.hidden=YES;
    CGRect tableFrame = _tableView.frame;
    tableFrame.origin.y = tableFrame.origin.y - CGRectGetHeight(_statusView.frame);
    tableFrame.size.height = tableFrame.size.height + CGRectGetHeight(_statusView.frame);
    _tableView.frame = tableFrame;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataService.dataList count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ResidentProgressListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    AdvisoryListCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"AdvisoryListCell" owner:self options:nil] objectAtIndex:0];
    if (item.ywlshShow && item.ywlshShow != (id)[NSNull null]) {
        cell.codeLabel.text = item.ywlshShow;
    } else {
        cell.codeLabel.text = @"";
    }
    cell.statusLabel.text = item.ywzt;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ResidentProgressDetailViewController *detailVC = [[ResidentProgressDetailViewController alloc] initWithNibName:@"ResidentProgressDetailViewController" bundle:nil];
    detailVC.listItem = [_dataService.dataList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - Search Bar Delegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"search bar input:%@",searchText);
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"search button clicked with input:%@",searchBar.text);
    [searchBar resignFirstResponder];
    [self doSearch:nil];
}

#pragma mark - PullToRefresh Method
//捕获到拉动事件
-(void)customPullToRefreshShouldRefresh:(CustomPullToRefresh *)ptr withDirection:(MSRefreshDirection)direction
{
    NSLog(@"捕获到拉动事件");
    if (direction == MSRefreshDirectionTop) {
        if (_searchBarName.text.length == 0 || _searchBarNo.text.length == 0) {
            //没有输入内容，什么都不处理
            [self.ptr endRefresh];
        } else {
            [_dataService requestResidentPeogressWithName:_searchBarName.text ID:_searchBarNo.text];
        }
    }
    if (direction == MSRefreshDirectionBottom) {
        if (_searchBarName.text.length == 0 || _searchBarNo.text.length == 0) {
            //没有输入内容，什么都不处理
            [self.ptr endRefresh];
        } else {
            [_dataService requestMoreResidentPeogressWithName:_searchBarName.text ID:_searchBarNo.text];        }
    }
}

#pragma mark - Data Service
-(void)loadDataFinished
{
    //搜索到结果后展示状态条
    if (self.statusView.hidden) {
        self.statusView.hidden = NO;
        CGRect tableFrame = _tableView.frame;
    tableFrame.origin.y = tableFrame.origin.y + CGRectGetHeight(_statusView.frame);
    tableFrame.size.height = tableFrame.size.height - CGRectGetHeight(_statusView.frame);
    _tableView.frame = tableFrame;
    }
    
    [self stopLoadingView];
    [self.ptr endRefresh];
    [self.tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    [self.ptr endRefresh];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)setEnableToLoadMore:(BOOL)enable
{
    self.ptr.enablePullToBottom = enable;
}

#pragma mark - Button
- (IBAction)doSearch:(id)sender
{
    [_searchBarName resignFirstResponder];
    [_searchBarNo resignFirstResponder];
    if (_searchBarName.text.length == 0) {
        [_searchBarName becomeFirstResponder];
        [self showAlert:@"请输入姓名"];
        return;
    }
    if (_searchBarNo.text.length == 0) {
        [_searchBarNo becomeFirstResponder];
        [self showAlert:@"请输入申办流水号"];
        return;
    }
    [self startLoadingView];
    [_dataService requestResidentPeogressWithName:_searchBarName.text ID:_searchBarNo.text];
}

-(void)showAlert:(NSString *) msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
@end
